import { myMessageList } from '@/api/message.js'

export const message = {
  namespaced: true,
  state: {
    unreadCount: 0,
    isNewMsg: false
  },
  mutations: {
    setUnreadCount(state, num) {
      state.unreadCount = num
    },
    isNewMessage(state, bol) {
      state.isNewMsg = bol
    }
  },
  actions: {
    // 获取未读消息数量
    async getUnreadMsgNum({ commit, state, rootGetters }, types) {
      console.log(rootGetters)
      var res = await myMessageList({ types: '1', username: rootGetters['user/userInfo'].userName })
      if (res.code === 0) {
        state.unreadCount = res.data.total
        commit('setUnreadCount', res.data.total)
        return state.unreadCount
      }
    }
  },
  getters: {
    getUnreadCount(state) {
      return state.unreadCount || 0
    },
    getIsNewMsg(state) {
      return state.isNewMsg
    }
  }
}
